package br.com.linkin.mi.utils;

import br.com.linkin.mi.contants.Constant;
import br.com.linkin.mi.utils.log.Logger;
import br.com.linkin.mi.utils.validation.BaseValidation;
import java.io.*;

/**
 *
 * @author daniel.mendonca
 */
public class Properties {
    
    /**
     * get Properties from *.properties
     * @param fileName
     * @param id
     * @return 
     */
    private static String get(String fileName, String id){        
        String propertie, value = "";
        try {            
            InputStream file = Properties.class.getResourceAsStream(fileName);            
            DataInputStream dis = new DataInputStream(file);            
            BufferedReader in = new BufferedReader(new InputStreamReader(dis));
            String line;
            while (in.ready()) {
                line = in.readLine();
                if(!BaseValidation.isInvalid(line)){
                    propertie = line.split(Constant.DELIMITADOR)[0];
                    value = line.split(Constant.DELIMITADOR)[1];
                    if (propertie.equals(id)) {
                        return value;
                    }
                }
            }
            
            Logger.erro("A propriedade \"" + id + "\" não foi encontrada no arquivo \"" +fileName+ "\"!"  );
            return id;
            
        } catch (IOException ex) {
            Logger.erro(Properties.class.getName(), ex);
        } 
        return value;
    }
    
    /**
     * get Properties from config.properties
     * @param conf
     * @return 
     */
    public static String config(String conf) {
        return Properties.get(Constant.CONFIG_FILE, conf);
    }
    
}